<?php
namespace Modules\Uploads\Controllers;

class IndexController extends \Phalcon\Mvc\Controller {
	public function initialize() {
		//$this -> response -> setStatusCode(200, 'OK') -> sendHeaders() -> setContentType('application/json', 'UTF-8');
	}

	public function indexAction() {
		if($this -> request -> hasFiles()) {
			$uploadFileName = date('Y').'/'.date('m').'/'.date('d');
			$filename = PUBLIC_PACH . 'u/'.$uploadFileName;
			if(!is_dir($filename)) {
				if(!mkdir($filename, 0777, true)) {
					return $this -> response -> setJsonContent(['code'=>-1,'msg'=>'上传目录创建失败']);
				}
			}
			$random = new \Phalcon\Security\Random();
			$tempFiles = [];

			$files = $this -> request -> getUploadedFiles();
			foreach($files as $file) {
				if($file -> isUploadedFile()) {
					$name = $random -> hex(10) . '.' . $file -> getExtension();
					if($file->moveTo($filename . '/' .  $name)) {
						$tempFiles[] = $uploadFileName . '/' .$name;
					}
				}
			}
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'上传成功','files'=>$tempFiles]);
	}
}